package com.interview.javabasic.Search;/*
@李子宁
Happy,happy everyday!
冲鸭！
*/

public class InsertValSearch {
    public static void main(String[] args) {
        int arr[] = new int[100];
        for (int i = 0; i < 100; i++) {
            arr[i] = i+1;
        }
        int index = insertVal(arr,0,arr.length-1,100);
        System.out.println("index= "+index);

    }
    public static int insertVal(int arr[],int left,int right,int findVal){
        if (left >right || findVal <arr[0] || findVal > arr[arr.length-1]){
            return -1;
        }
        //求出mid
        int mid = left + (right-left)*(findVal - arr[left])/(arr[right]-arr[left]);
        int midVal = arr[mid];
        if (findVal > midVal){
            return insertVal(arr,mid+1,right,findVal);
        }else if (findVal < arr[mid]){
            return insertVal(arr,left,mid-1,findVal);
        }else{
            return mid;
        }

    }
}


